﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DoAnB
{
    public partial class frmDoiMaPIN : Form
    {
        public frmDoiMaPIN()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string sql = "Select * from NguoiDung Where MaNV=" + Session._MaNV + "AND MatKhau =" +txtMK.Text;
            ketnoi kn = new ketnoi();
            if (!kn.ExecuteReader(sql).HasRows)
            {
                MessageBox.Show("Mật khẩu không đúng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtMK.Clear();
                txtMK.Focus();
            }
            else
            {
                kn.disconnect();
                Random r = new Random();
                string pin = r.Next(1, 9998).ToString("D4");
                string checkPinSql = "Select * from NguoiDung Where PIN ='" + pin + "'";
                while (kn.ExecuteReader(checkPinSql).HasRows)
                    pin = r.Next(1, 9998).ToString("D4");
                sql = "update NguoiDung Set PIN='" + pin + "' Where MaNV =" + Session._MaNV;
                kn.ExeCuteNonQuery(sql);
                MessageBox.Show("Hãy ghi nhớ mã PIN mới của bạn: " + pin, "Chú ý", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
    }
}
